package com.example.juc;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

class Book {
    private String title;
    private double price;

    public Book(String title, double price) {
        this.title = title;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return Double.compare(price, book.price) == 0 && Objects.equals(title, book.title);
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, price);
    }
}
public class ReferenceTest {
    public static void main(String[] args) {
        Book book = new Book("Java JUC", 29.9);
        System.out.println(book);
        AtomicReference ref = new AtomicReference(book);
        ref.compareAndSet(book, new Book("Spring", 39.9));
        System.out.println(ref);
        // 匿名对象无法被正常替换
        ref.compareAndSet(new Book("Java JUC", 29.9), new Book("Mysql", 19.9));
        System.out.println(ref);
    }
}
